﻿using System;
using System.Text;
using DevExpress.Xpf.Core;
using System.Security.Cryptography;
using ChatApp.Client.ChatAppServiceReference;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DevExpress.Xpf.Docking;

namespace ChatApp.Client.Common
{
    public static class ChatAppGlobal
    {
        public static User UserLogin { get; set; }

        public static List<AddFriend> ListAddFriend { get; set; }

        public static int SoLuongRoom { get; set; }

        private static DXWindow formLogin;
        /// <summary>
        /// Quản lý form Login
        /// </summary>
        public static DXWindow FormLogin
        {
            get
            {
                if (formLogin == null)
                {
                    return new DXWindow();
                }
                return formLogin;
            }
            set
            {
                formLogin = value;
            }
        }

        private static DXWindow formMainWindow;
        /// <summary>
        /// Quản lý form MainWindow
        /// </summary>
        public static DXWindow FormMainWindow
        {
            get
            {
                if (formMainWindow == null)
                {
                    return new DXWindow();
                }
                return formMainWindow;
            }
            set
            {
                formMainWindow = value;
            }
        }

        public static List<BaseLayoutItem> ListTabChat
        {
            get
            {
                List<BaseLayoutItem> listLayout = new List<BaseLayoutItem>();
                // Lấy tất cả Tab trên mainwindow ra kiểm tra có tồn tại tên room không
                MainWindow mainWindown = FormMainWindow as MainWindow;
                BaseLayoutItemCollection layoutCols = mainWindown.dockLayoutManagerMain.LayoutRoot.Items;
                if (layoutCols.Count == 1)
                {
                    TabbedGroup tabGroup = mainWindown.dockLayoutManagerMain.LayoutRoot.Items.FirstOrDefault(u=>u.Name == "Tabb") as TabbedGroup;
                    if (tabGroup == null)
                    {
                        listLayout.Add(mainWindown.dockLayoutManagerMain.LayoutRoot.Items.FirstOrDefault());
                    }
                    else
                    {
                        listLayout = tabGroup.Items.ToList();
                    }
                }
                return listLayout;
            }
        }

        #region Method

        public static string EncodeMD5(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes).Replace("-","").ToLower();
        }

        #endregion
    }
}
